﻿<%@ WebHandler Language="C#" Class="CustomerMgr" %>

using System;
using System.Web;
using System.Data;
using System.Text.RegularExpressions;
using Chenxin.Lib.Utility;


public class CustomerMgr : AjaxHandlerBase
{
    public CustomerMgr()
    {
        htTypePermit.Add("view", Constants.Permit_Customer_View);
        htTypePermit.Add("edit", Constants.Permit_Customer_Edit);
        htTypePermit.Add("setpublevel", Constants.Permit_SetCustomerPubLevel);
        htTypePermit.Add("allotpubcostomer", Constants.Permit_AllotPubCustomer);
    }

    public override void RequestProcess(HttpContext context)
    {
        context.Response.AddHeader("pragma", "no-cache");
        context.Response.AddHeader("cache-control", "");
        context.Response.CacheControl = "no-cache";
        
        if (context.Request["op"] != null)
        {
            switch (context.Request["op"].ToLower())
            {
                case "getlist":
                    GetList(context);
                    break;
                case "getmodel":
                    GetModel(context);
                    break;
                case "edit":
                    Edit(context);
                    break;
                case "setpublevel":
                    SetPubLevel(context);
                    break;
                case "shiftpubcustomer":
                    ShiftPubCustomer(context);
                    break;
                case "getinfo":
                    GetInfo(context);
                    break;      
            }
        }
    }

    /// <summary>
    /// 获取列表
    /// </summary>
    /// <param name="context"></param>
    private void GetList(HttpContext context)
    {
        System.Collections.Hashtable htParams = new System.Collections.Hashtable();
        string strParam;
        string strSort = context.Request["sort"].ToString();
        string sort = string.Empty;
        if (!string.IsNullOrEmpty(strSort))
        {
            sort = " " + strSort;
        }

        if (!string.IsNullOrEmpty(context.Request["userid"]))
        {
            htParams.Add("UserId", context.Request["userid"]);
        }
        else if (!string.IsNullOrEmpty(context.Request["departid"]))
        {
            htParams.Add("DepartId", context.Request["departid"]);
        }
        else if (!string.IsNullOrEmpty(context.Request["areaid"]))
        {
            htParams.Add("AreaId", context.Request["areaid"]);
        }
        else if (!string.IsNullOrEmpty(context.Request["companyid"]))
        {
            htParams.Add("CompanyId", context.Request["companyid"]);
        }
        else if (!string.IsNullOrEmpty(context.Request["cityid"]))
        {
            htParams.Add("CityId", context.Request["cityid"]);
        }
        else
        {
            htParams.Add("CompanyId", CurrentUserInfo.CompanyId);
        }
        
        strParam = context.Request.Params["CustomerName"];
        if (strParam != null)
        {
            htParams.Add("CustomerName", strParam.ToString().Trim());
        }
        strParam = context.Request.Params["txtCustomerKind"];
        if (!string.IsNullOrEmpty(strParam))
        {
            htParams.Add("CustomerKind", strParam.ToString().Trim());
        }
        strParam = context.Request.Params["txtCustomerPubLevel"];
        if (!string.IsNullOrEmpty(strParam))
        {
            htParams.Add("CustomerPubLevel", strParam.ToString().Trim());
        }
        strParam = context.Request.Params["txtcustomerphone"];
        if (!string.IsNullOrEmpty(strParam))
        {
            htParams.Add("txtcustomerphone", strParam.ToString().Trim());
        }
        //权限判断
        
        //查看所有私客户权限
        if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewAllPrivCustomer))
        {
            htParams.Add("Permit_Priv", Constants.Permit_ViewAllPrivCustomer);
        }
        //查看城市私客户权限
        else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewCityPrivCustomer))
        {
            htParams.Add("Permit_Priv", Constants.Permit_ViewCityPrivCustomer);
            htParams.Add("CurCityId_Priv", CurrentUserInfo.CityId);
        }
        //查看公司私客户权限
        else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewCompanyPrivCustomer))
        {
            htParams.Add("Permit_Priv", Constants.Permit_ViewCompanyPrivCustomer);
            htParams.Add("CurCompanyId_Priv", CurrentUserInfo.CompanyId);
        }
        //查看部门私客户权限
        else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewDepartPrivCustomer))
        {
            htParams.Add("Permit_Priv", Constants.Permit_ViewDepartPrivCustomer);
            htParams.Add("CurUserId_Priv", CurrentUserInfo.UserId);
        }
        //查看自己权限    
        else
        {
            htParams.Add("CurUserId_Priv", CurrentUserInfo.UserId);
        }

        //查看所有公客户权限
        if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewAllPubCustomer))
        {
            htParams.Add("Permit_Pub", Constants.Permit_ViewAllPubCustomer);
        }
        //查看城市公客户权限
        else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewCityPubCustomer))
        {
            htParams.Add("Permit_Pub", Constants.Permit_ViewCityPubCustomer);
            htParams.Add("CurCityId_Pub", CurrentUserInfo.CityId);
        }
        //查看公司公客户权限
        else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewCompanyPubShareCustomer))
        {
            htParams.Add("Permit_Pub", Constants.Permit_ViewCompanyPubShareCustomer);
            htParams.Add("CurCompanyId_Pub", CurrentUserInfo.CompanyId);
        }
        //查看跨部门公客户权限
        else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewCrossDepartPubShareCustomer))
        {
            htParams.Add("Permit_Pub", Constants.Permit_ViewCrossDepartPubShareCustomer);
            htParams.Add("CurUserId_Pub", CurrentUserInfo.UserId);
            htParams.Add("CurCompanyId_Pub", CurrentUserInfo.CompanyId);
            htParams.Add("CurDepartId_Pub", CurrentUserInfo.DepartId);
        }
        //查看本部门公客户权限
        else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewDepartPubShareCustomer))
        {
            htParams.Add("Permit_Pub", Constants.Permit_ViewDepartPubShareCustomer);
            htParams.Add("CurCompanyId_Pub", CurrentUserInfo.CompanyId);
            htParams.Add("CurDepartId_Pub", CurrentUserInfo.DepartId);
        }
        //查看自己权限    
        else
        {
            htParams.Add("CurUserId_Pub", CurrentUserInfo.UserId);
        }

        htParams.Add("OprPersonId", CurrentUserInfo.UserId);
        
        strParam = context.Request.Params["pagesize"];
        if (strParam != null)
        {
            htParams.Add("PageSize", strParam);
        }
        strParam = context.Request.Params["pageindex"];
        if (strParam != null)
        {
            htParams.Add("PageIndex", strParam);
        }
        
        string strRet;
        try
        {
            //DataSet ds = Chenxin.Lib.House.DAL.Customer_BaseInfo.GetList(pageIndex, pageSize, order, strWhere, "*,(case sex when 20 then '女' else '男' end) as CustomerSex", "*,(case sex when 20 then '女' else '男' end) as CustomerSex", true);
            //count = Convert.ToInt32(ds.Tables[1].Rows[0].ItemArray[0].ToString());
            Chenxin.Lib.House.BLL.Customer_BaseInfo bllCustomer = new Chenxin.Lib.House.BLL.Customer_BaseInfo();
            DataSet ds = new DataSet();
            if (string.IsNullOrEmpty(sort.Trim()))
            {
                ds = bllCustomer.getListByParams(htParams);
            }
            else
            {
                ds = bllCustomer.getList(htParams, sort);
            }
            int count = Convert.ToInt32(ds.Tables[1].Rows[0].ItemArray[0]);

            context.Response.Write(string.Format("{{success:1,msg:'',data:{{count:{0},{1}}}}}", count, JsonHelp.Convert2Json(ds.Tables[0])));
            
            
            
            //strRet = retExecResult(Constants.ExecResult.Succ, "", CreateGrid(ds));
            //context.Response.Write(strRet);
            //context.Response.Write(string.Format("{{success:'1',data:{{data:'{0}',count:{1}}}}}", CreateGrid(ds.Tables[0].Select("1=1")), count));
        }
        catch (Exception ex)
        {
            strRet = retExecResult(Constants.ExecResult.Error, ex.Message, "");
            context.Response.Write(strRet);
        }
    }


    /// <summary>
    /// 生成列表
    /// </summary>
    /// <param name="drList"></param>
    /// <returns></returns>
    public Constants.Pagination CreateGrid(DataSet ds)
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        string strLastTraceDay = string.Empty;
        string style = string.Empty;
        Chenxin.Lib.House.Entity.House_BusinessSetting entSetting;
        
        sb.Append("<table style=\"width:100%; height:100%;\" cellspacing=\"1\" class=\"tabsorternew\" id=\"report\">");
        sb.Append("<thead>");
        sb.Append("<tr>");
        sb.Append("<th width=\"65px\" >客户姓名</th>");
        sb.Append("<th width=\"65px\" >客户类别</th>");
        sb.Append("<th width=\"180px\">客户电话</th>");
        //sb.Append("<th width=\"80px\">客户手机</th>");
        sb.Append("<th width=\"80px\">操作部门</th>");
        sb.Append("<th width=\"80px\">操作人</th>");
        sb.Append("<th width=\"80px\">公开级别</th>");
        sb.Append("<th>最后跟盘日期</th>");
        sb.Append("<th width=\"80px\" >建档日期</th>");
        //sb.Append("<th width=\"30px\">性别</th>");
        //sb.Append("<th width=\"70px\">国籍</th>");
        //sb.Append("<th width=\"80px\">证件类型</th>");
        //sb.Append("<th width=\"90px\">证件号</th>");
        //sb.Append("<th width=\"150px\">地址</th>");
        sb.Append("</tr></thead>");
        sb.Append("<tbody>");

        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            //Chenxin.Lib.House.Entity.House_BusinessSetting model = Chenxin.Lib.House.DAL.House_BusinessSetting.GetModel(new Guid(dr["OprDepartId"].ToString()));
            //if (dr["LastTraceDay"]!=null && !String.IsNullOrEmpty(dr["LastTraceDay"].ToString())) { 
            //   strLastTraceDay = Convert.ToDateTime(dr["LastTraceDay"]).ToString("yyyy-MM-dd");
            //   TimeSpan ts = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd")).Subtract(Convert.ToDateTime(strLastTraceDay));

            //   if (model.OverTimeTraceNum - ts.Days <= 2 && "20".Equals(dr["CustomerKind"]) && model.OverTimeTraceNum != 0)
            //   {
            //       style = "color:red";
            //   }
            //}
            strLastTraceDay = string.Empty;
            style = string.Empty;
            //客需私客户
            if (dr["CustomerKind"].ToString().Equals("20") && dr["PubLevel"].ToString().Equals("私客"))
            {
                entSetting = Chenxin.Lib.House.DAL.House_BusinessSetting.GetModel(new Guid(dr["OprDepartId"].ToString()));
                //无限制
                if (entSetting.OverTimeTraceNum != null)
                {
                    if (entSetting.OverTimeTraceNum.Value != 0)
                    {
                        //最后跟单时间为空
                        if (String.IsNullOrEmpty(dr["LastTraceDay"].ToString()))
                        {
                            style = "color:red";
                        }
                        else
                        {
                            strLastTraceDay = Convert.ToDateTime(dr["LastTraceDay"]).ToString("yyyy-MM-dd");
                            TimeSpan ts = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd")).Subtract(Convert.ToDateTime(strLastTraceDay));
                            if (entSetting.OverTimeTraceNum - ts.Days <= 2)
                            {
                                style = "color:red";
                            }
                        }
                    }
                }
            }
            sb.AppendFormat("<tr id=\"{0}\" class=\"tr_customer\">", dr["CustomerID"]);
            sb.AppendFormat("<td style=\"{0}\">{1}</td>", style, dr["CustomerName"]);
            sb.AppendFormat("<td style=\"{0}\">{1}</td>", style, dr["Kind"]);
            sb.AppendFormat("<td style=\"{0}\">{1}</td>", style, StringJoin(dr["Phone"].ToString(), dr["Mobile"].ToString()));
            //sb.AppendFormat("<td style=\"{0}\">{1}</td>", style, dr["Mobile"]);
            sb.AppendFormat("<td style=\"{0}\">{1}</td>", style, dr["OprDepart"]);
            sb.AppendFormat("<td style=\"{0}\">{1}</td>", style, dr["OprPerson"]);
            sb.AppendFormat("<td style=\"{0}\">{1}</td>", style, dr["PubLevel"]);
            sb.AppendFormat("<td style=\"{0}\">{1}</td>", style, dr["LastTraceDay"]);
            if (dr["Adddate"] != null && !String.IsNullOrEmpty(dr["Adddate"].ToString()))
            {
                sb.AppendFormat("<td style=\"{0}\">{1:yyyy-MM-dd}</td>", style, Convert.ToDateTime(dr["Adddate"]));
            }
            else {
                sb.AppendFormat("<td style=\"{0}\">{1:yyyy-MM-dd}</td>", style, "");
            }
            //sb.AppendFormat("<td style=\"{0}\">{1}</td>", style, dr["CustomerSex"]);
            //sb.AppendFormat("<td style=\"{0}\">{1}</td>", style, dr["Nationality"]);
            //sb.AppendFormat("<td style=\"{0}\">{1}</td>", style, dr["CertificateType"]);
            //sb.AppendFormat("<td style=\"{0}\">{1}</td>", style, dr["CertificateNo"]);
            //sb.AppendFormat("<td style=\"{0}\">{1}</td>", style, dr["Address"]);
            sb.Append("</tr>");
        }
        sb.Append("</tbody>");
        sb.Append("</table>");

        Constants.Pagination stuPag = new Constants.Pagination();
        stuPag.data = sb.ToString();
        stuPag.count = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
        return stuPag;
    }

    private string StringJoin(string strFirst, string strSecond)
    {
        if (strFirst == null)
        {
            strFirst = String.Empty;
        }
        if (strSecond == null)
        {
            strSecond = String.Empty;
        }

        if (String.IsNullOrEmpty(strFirst) || String.IsNullOrEmpty(strSecond))
        {
            return strFirst + strSecond;
        }
        else
        {
            return strFirst + "," + strSecond;
        }
    }

    /// <summary>
    /// 编辑
    /// </summary>
    /// <param name="context"></param>
    private void Edit(HttpContext context)
    {
        try
        {
            //客源基本信息实体对象
            Chenxin.Lib.House.Entity.Customer_BaseInfo model = (Chenxin.Lib.House.Entity.Customer_BaseInfo)JsonHelp.Convert2Entity(context.Request["model"], typeof(Chenxin.Lib.House.Entity.Customer_BaseInfo));
            //更新之前信息
            Chenxin.Lib.House.Entity.Customer_BaseInfo oldCustomerBase = Chenxin.Lib.House.DAL.Customer_BaseInfo.GetModel(model.CustomerId);

            //数据变更
            if (oldCustomerBase == null)
            {
                context.Response.Write(retExecResult(Constants.ExecResult.Error, "数据已变更，请刷新后重新执行操作！", ""));
                return;
            }
            else
            {
                //设置客源基本信息
                oldCustomerBase.LastTraceDay = DateTime.Now;                              //最后跟进日期
                oldCustomerBase.CustomerName = model.CustomerName;                        //客户姓名
                oldCustomerBase.Address = model.Address;                                  //联系地址
                oldCustomerBase.Mobile = model.Mobile;                                    //手机
                oldCustomerBase.Phone = model.Phone;                                      //电话
                oldCustomerBase.Nationality = model.Nationality;                          //国籍
                oldCustomerBase.Sex = model.Sex;                                          //性别
                oldCustomerBase.CertificateType = model.CertificateType;                  //证件类型
                oldCustomerBase.CertificateNo = model.CertificateNo;                      //证件号码
                
                Chenxin.Lib.House.Entity.House_TraceInfo house_TraceInfo = new Chenxin.Lib.House.Entity.House_TraceInfo();
                house_TraceInfo.TractorId = CurrentUserInfo.UserId;
                house_TraceInfo.Tractor = CurrentUserInfo.UserName;
                house_TraceInfo.Depart = CurrentUserInfo.DepartName;
                house_TraceInfo.DepartId = CurrentUserInfo.DepartId;
                house_TraceInfo.CityId = CurrentUserInfo.CityId;
                house_TraceInfo.CityName = CurrentUserInfo.CityName;
                house_TraceInfo.CompanyId = CurrentUserInfo.CompanyId;
                house_TraceInfo.CompanyName = CurrentUserInfo.CompanyName;
                //添加客源信息
               Chenxin.Lib.House.BLL.Custom_Info.UpdateCustomBaseInfo(oldCustomerBase, house_TraceInfo);
               string text = CurrentUserInfo.UserName + "：编辑。客户姓名：[" + oldCustomerBase.CustomerName + "]" + "证件类型：[" + oldCustomerBase.CertificateType + "]" + "证件号码：[" + oldCustomerBase.CertificateNo + "]";
               AddLog(text, BusinessType.BusinessTypeComboList()["OprType_EditCustomerMgr"].ToString());
            }
            context.Response.Write(("{success:'1',msg:'保存成功！',data:{CommunityId:'" + model.CustomerId + "'}}"));
        }
        catch
        {
            context.Response.Write(("{success:'0',msg:'保存失败！',data:''}"));
        }
    }

    /// <summary>
    /// 变更客户公开级别
    /// </summary>
    /// <param name="context"></param>
    private void SetPubLevel(HttpContext context)
    {
        try
        {
            Chenxin.Lib.House.Entity.Customer_BaseInfo model = (Chenxin.Lib.House.Entity.Customer_BaseInfo)JsonHelp.Convert2Entity(context.Request["model"], typeof(Chenxin.Lib.House.Entity.Customer_BaseInfo));

            Chenxin.Lib.House.Entity.Customer_BaseInfo customer_baseInfo = Chenxin.Lib.House.DAL.Customer_BaseInfo.GetModel(model.CustomerId);
            if (customer_baseInfo == null)
            {
                context.Response.Write(retExecResult(Constants.ExecResult.Error, "客户标识丢失，请联系管理员！", ""));
                return;
            }
            customer_baseInfo.PubLevel = model.PubLevel;
            //Chenxin.Lib.House.DAL.Customer_BaseInfo.Update(customer_baseInfo);

            Chenxin.Lib.House.Entity.House_TraceInfo entTraceInfo = new Chenxin.Lib.House.Entity.House_TraceInfo();
            entTraceInfo.TractorId = CurrentUserInfo.UserId;
            entTraceInfo.Tractor = CurrentUserInfo.UserName;
            entTraceInfo.Depart = CurrentUserInfo.DepartName;
            entTraceInfo.DepartId = CurrentUserInfo.DepartId;
            entTraceInfo.CityId = CurrentUserInfo.CityId;
            entTraceInfo.CityName = CurrentUserInfo.CityName;
            entTraceInfo.CompanyId = CurrentUserInfo.CompanyId;
            entTraceInfo.CompanyName = CurrentUserInfo.CompanyName;
            //添加客源信息
            Chenxin.Lib.House.BLL.Custom_Info.UpdateCustomBaseInfo(customer_baseInfo, entTraceInfo);
            
            context.Response.Write(retExecResult(Constants.ExecResult.Succ, "变更成功!", ""));
        }
        catch(Exception ex)
        {
            context.Response.Write(retExecResult(Constants.ExecResult.Error, ex.Message, ""));
        }
    }

    /// <summary>
    /// 转移公开客源
    /// </summary>
    /// <param name="context"></param>
    private void ShiftPubCustomer(HttpContext context)
    {
        try
        {
            
            Chenxin.Lib.House.Entity.Customer_BaseInfo model = (Chenxin.Lib.House.Entity.Customer_BaseInfo)JsonHelp.Convert2Entity(context.Request["model"], typeof(Chenxin.Lib.House.Entity.Customer_BaseInfo));
            
            Chenxin.Lib.House.Entity.Customer_BaseInfo customer_baseInfo = Chenxin.Lib.House.DAL.Customer_BaseInfo.GetModel(model.CustomerId);
            if (customer_baseInfo == null)
            {
                context.Response.Write(retExecResult(Constants.ExecResult.Error, "客户标识丢失，请联系管理员！", ""));
                return;
            }
            customer_baseInfo.PubLevel = 0;
            customer_baseInfo.CityId = model.CityId;
            customer_baseInfo.CityName = model.CityName;
            customer_baseInfo.CompanyId = model.CompanyId;
            customer_baseInfo.CompanyName = model.CompanyName;
            customer_baseInfo.OprDepartId = model.OprDepartId;
            customer_baseInfo.OprDepart = model.OprDepart;
            customer_baseInfo.OprPersonId = model.OprPersonId;
            customer_baseInfo.OprPerson = model.OprPerson;

            SetCostomerDemandPubLevel(customer_baseInfo);

            Chenxin.Lib.House.Entity.House_TraceInfo entTraceInfo = new Chenxin.Lib.House.Entity.House_TraceInfo();
            entTraceInfo.TractorId = CurrentUserInfo.UserId;
            entTraceInfo.Tractor = CurrentUserInfo.UserName;
            entTraceInfo.Depart = CurrentUserInfo.DepartName;
            entTraceInfo.DepartId = CurrentUserInfo.DepartId;
            entTraceInfo.CityId = CurrentUserInfo.CityId;
            entTraceInfo.CityName = CurrentUserInfo.CityName;
            entTraceInfo.CompanyId = CurrentUserInfo.CompanyId;
            entTraceInfo.CompanyName = CurrentUserInfo.CompanyName;
            //添加客源信息
            Chenxin.Lib.House.BLL.Custom_Info.UpdateCustomBaseInfo(customer_baseInfo, entTraceInfo);
            
            //Chenxin.Lib.House.DAL.Customer_BaseInfo.Update(customer_baseInfo);

            context.Response.Write(retExecResult(Constants.ExecResult.Succ, "转移客源成功!", ""));

        }
        catch(Exception ex)
        {
            context.Response.Write(retExecResult(Constants.ExecResult.Error, ex.Message, ""));
        }
    }

    /// <summary>
    /// 更新客需信息
    /// </summary>
    /// <param name="customer_baseInfo"></param>
    public void SetCostomerDemandPubLevel(Chenxin.Lib.House.Entity.Customer_BaseInfo customer_baseInfo)
    {
        System.Collections.Generic.List<Chenxin.Lib.House.Entity.Customer_DemandInfo> list = Chenxin.Lib.House.DAL.Customer_DemandInfo.GetListArray(string.Format(" CustomerId = '{0}'", customer_baseInfo.CustomerId));
        foreach (Chenxin.Lib.House.Entity.Customer_DemandInfo model in list)
        {
            model.PubLevel = customer_baseInfo.PubLevel;
            model.CityId = customer_baseInfo.CityId;
            model.CityName = customer_baseInfo.CityName;
            model.CompanyId = customer_baseInfo.CompanyId;
            model.CompanyName = customer_baseInfo.CompanyName;
            model.OprDepartId = customer_baseInfo.OprDepartId;
            model.OprDepart = customer_baseInfo.OprDepart;
            model.OprPersonId = customer_baseInfo.OprPersonId;
            model.OprPerson = customer_baseInfo.OprPerson;

            Chenxin.Lib.House.Entity.House_TraceInfo entTraceInfo = new Chenxin.Lib.House.Entity.House_TraceInfo();
            entTraceInfo.TractorId = CurrentUserInfo.UserId;
            entTraceInfo.Tractor = CurrentUserInfo.UserName;
            entTraceInfo.Depart = CurrentUserInfo.DepartName;
            entTraceInfo.DepartId = CurrentUserInfo.DepartId;
            entTraceInfo.CityId = CurrentUserInfo.CityId;
            entTraceInfo.CityName = CurrentUserInfo.CityName;
            entTraceInfo.CompanyId = CurrentUserInfo.CompanyId;
            entTraceInfo.CompanyName = CurrentUserInfo.CompanyName;
            //添加客源信息
            string strTemp = Chenxin.Lib.House.BLL.Custom_Info.UpdateCustomDemandInfo(model, entTraceInfo);
            
            //Chenxin.Lib.House.DAL.Customer_DemandInfo.Update(model);
        }    
    }

    /// <summary>
    /// 获取实体,不显示 
    /// </summary>
    /// <param name="context"></param>
    private void GetInfo(HttpContext context)
    {
        try
        {
            Chenxin.Lib.House.Entity.Customer_BaseInfo model = null;
            if (context.Request["modelid"] != null)
            {
                string id = context.Request["modelid"];
                model = Chenxin.Lib.House.DAL.Customer_BaseInfo.GetModel(new Guid(id));
                if (model == null)
                {
                    context.Response.Write(("{success:'0',msg:'获取记录信息失败，未找到对应客户信息！',data:''}"));
                    return;
                }
                context.Response.Write("{success:'1',msg:'',data:{model:" + JsonHelp.Convert2Json(model) + "}}");
            }
        }
        catch
        {
            context.Response.Write(("{success:'0',msg:'获取记录信息失败！',data:''}"));
        }
    }
    
    /// <summary>
    /// 获取实体
    /// </summary>
    /// <param name="context"></param>
    private void GetModel(HttpContext context)
    {
        try
        {
            Chenxin.Lib.House.Entity.Customer_BaseInfo model = null;
            Chenxin.Lib.House.Entity.House_BaseInfo house = null;
            if (context.Request["modelid"] != null)
            {
               string id = context.Request["modelid"];
               //查询房源页面和请求变更页面中的业主查看
               string flg = context.Request["flg"];
               string temp = string.Empty;
               bool writelog = false;
               if ("house".Equals(flg))
               {
                   house = Chenxin.Lib.House.DAL.House_BaseInfo.GetModel(new Guid(id));
                   model = Chenxin.Lib.House.DAL.Customer_BaseInfo.GetModel(house.CustomerId);
                   if (house==null)
                   {
                       context.Response.Write(("{success:'0',msg:'获取记录信息失败，未找到对应房源信息！',data:''}"));
                       return;
                   }
                   if (model == null)
                   {
                       context.Response.Write(("{success:'0',msg:'获取记录信息失败，未找到对应客户信息！',data:''}"));
                       return;
                   }
                   temp = check(house, model,out writelog);
                   if (!writelog) {
                       temp = checkRegion(house, model, out writelog);
                   }
               } 
               //客源和客需页面中的业主查看
               else {
                   model = Chenxin.Lib.House.DAL.Customer_BaseInfo.GetModel(new Guid(id));
                   if (model == null)
                   {
                       context.Response.Write(("{success:'0',msg:'获取记录信息失败，未找到对应客户信息！',data:''}"));
                       return;
                   }
                   //temp = customercheck(model, out writelog);
                   writelog = true;
                   temp = "{success:'1',msg:'',data:{model:" + JsonHelp.Convert2Json(model) + "}}";
               }
               //写log
               if (writelog) {
                       Chenxin.Lib.House.Entity.House_ViewOwerInfo viewOwnerInfo = new Chenxin.Lib.House.Entity.House_ViewOwerInfo();
                       viewOwnerInfo.RecordId = Guid.NewGuid();
                       if ("house".Equals(flg))
                       {
                           viewOwnerInfo.HouseId = house.HouseId;
                           viewOwnerInfo.HouseCode = house.HouseCode;
                           viewOwnerInfo.HouseAddress = house.Community + house.BuildingName + house.Room;
                           viewOwnerInfo.Status = 0;
                       }
                       else { 
                           viewOwnerInfo.HouseId =model.CustomerId;
                           viewOwnerInfo.HouseCode =model.CustomerCode;
                           viewOwnerInfo.HouseAddress = model.CustomerName;
                           viewOwnerInfo.Status = 1;
                       }
                       viewOwnerInfo.ViewEmployeeId = CurrentUserInfo.UserId;
                       viewOwnerInfo.Employee = CurrentUserInfo.UserName;
                       viewOwnerInfo.ViewDepartId = CurrentUserInfo.DepartId;
                       viewOwnerInfo.Depart = CurrentUserInfo.DepartName;
                       viewOwnerInfo.Addtime = DateTime.Now;
                       Chenxin.Lib.House.DAL.House_ViewOwerInfo.Add(viewOwnerInfo);
               }
               context.Response.Write(temp);
            }
        }
        catch(Exception ex)
        {
            context.Response.Write(("{success:'0',msg:'获取记录信息失败！" + ex.Message +"',data:''}"));
        }
    }
    

    //*******************************************
    //功能概要       ：查业主电话逻辑
    //参数説明       ：Customer_BaseInfo
    //返回值説明     ：void
    //功能詳細       ：查业主电话逻辑
    //作成者         ：chen.yugong 
    //作成日         ：2010-05-20
    //修改者         ：
    //修改日         ：
    //*******************************************
    private string check(Chenxin.Lib.House.Entity.Customer_BaseInfo model,out bool writelog)
    { //是否是自己的房源,直接返回
        if (CurrentUserInfo.UserId.Equals(model.OprPersonId))
        {
            writelog = true;
            return "{success:'1',msg:'',data:{model:" + JsonHelp.Convert2Json(model) + "}}";
        }
        //非本人
        else
        {
            //1.有查所有权限
            if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewCustomer_All))
            {
                //有查所有和查真电话权限 直接返回
                if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewRealTel))
                {
                    writelog = true;
                    return "{success:'1',msg:'',data:{model:" + JsonHelp.Convert2Json(model) + "}}";
                }
                //有查所有和查假电话权限
                else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewNotRealTel))
                {
                    writelog = false;
                    return "{success:0,msg:'操作失败，您无此权限，请联系管理员！',data:{permit:false}}";
                } else {
                    writelog = false;
                    return "{success:0,msg:'权限设置错误，请联系管理员！',data:{permit:false}}";
                }

            }
            //2.有查城市权限
            else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewCustomer_City))
            {
                //不在同一城市
                if (!CurrentUserInfo.CityId.Equals(model.CityId))
                {
                    writelog = false;
                    return "{success:0,msg:'操作失败，您无此权限，请联系管理员！',data:{permit:false}}";
                }
                //在同一城市
                else
                {
                    //有查城市和查真电话权限 直接返回
                    if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewRealTel))
                    {
                        writelog = true;
                        return "{success:'1',msg:'',data:{model:" + JsonHelp.Convert2Json(model) + "}}";
                    }
                    //有查城市和查假电话权限 
                    else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewNotRealTel))
                    {
                        writelog = false;
                        return "{success:0,msg:'操作失败，您无此权限，请联系管理员！',data:{permit:false}}";
                    }
                    else
                    {
                        writelog = false;
                        return "{success:0,msg:'权限设置错误，请联系管理员！',data:{permit:false}}";
                    }
                }
            }
            //3.有查公司权限
            else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewCustomer_Company))
            {
                //不在同一公司
                if (!CurrentUserInfo.CompanyId.Equals(model.CompanyId))
                {
                    writelog = false;
                    return "{success:0,msg:'操作失败，您无此权限，请联系管理员！',data:{permit:false}}";
                }
                //在同一公司
                else
                {
                    //有查城市和查真电话权限 直接返回
                    if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewRealTel))
                    {
                        writelog = true;
                        return "{success:'1',msg:'',data:{model:" + JsonHelp.Convert2Json(model) + "}}";
                    }
                    //有查城市和查假电话权限 
                    else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewNotRealTel))
                    {
                        writelog = false;
                        return "{success:0,msg:'操作失败，您无此权限，请联系管理员！',data:{permit:false}}";
                    }
                    else
                    {
                        writelog = false;
                        return "{success:0,msg:'权限设置错误，请联系管理员！',data:{permit:false}}";
                    }
                }
            }
            else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewCustomer_Department))
            {
                bool isDepart = false;
               
                //查部门设定
                Chenxin.Lib.Hr.Entity.Struct_UserDepartRelation currentDep = null;
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                sb.AppendFormat("Userid = '{0}'and DepartId = '{1}' ", CurrentUserInfo.UserId, model.OprDepartId);
                System.Collections.Generic.List<Chenxin.Lib.Hr.Entity.Struct_UserDepartRelation> list = Chenxin.Lib.Hr.DAL.Struct_UserDepartRelation.GetListArray(sb.ToString());
                if (list.Count > 0)
                {
                    Chenxin.Lib.Hr.Entity.Struct_UserDepartRelation udr = list[0];
                    currentDep = udr;
                    isDepart = true;
                }
                
                //如果房源的部门在组织设定表中有
                if(isDepart) {
                    //查真电话权限 直接返回
                    if (currentDep.ViewRealCustomer)
                    {
                        writelog = true;
                        return "{success:'1',msg:'',data:{model:" + JsonHelp.Convert2Json(model) + "}}";
                    }
                    //查假电话权限 
                    else if (currentDep.ViewFakeCustomer)
                    {
                        writelog = true;
                        return "{success:0,msg:'操作失败，您无此权限，请联系管理员！',data:{permit:false}}";
                    }
                    else
                    {
                        writelog = false;
                        return "{success:0,msg:'权限设置错误，请联系管理员！',data:{permit:false}}";
                    }
                
                } 
                else {
                    writelog = false;
                    return "{success:0,msg:'操作失败，您无此权限，请联系管理员！',data:{permit:false}}";
                }
            }else {
                    writelog = false;
                    return "{success:0,msg:'操作失败，您无此权限，请联系管理员！',data:{permit:false}}";
                }
            }
    }
    
    //*******************************************
    //功能概要       ：查业主电话逻辑
    //参数説明       ：Customer_BaseInfo
    //返回值説明     ：void
    //功能詳細       ：查业主电话逻辑
    //作成者         ：chen.yugong 
    //作成日         ：2010-05-20
    //修改者         ：
    //修改日         ：
    //*******************************************
    private string check(Chenxin.Lib.House.Entity.House_BaseInfo house_BaseInfo, Chenxin.Lib.House.Entity.Customer_BaseInfo model,out bool writelog)
    {
        //是否是自己的房源,直接返回
        if (CurrentUserInfo.UserId.Equals(house_BaseInfo.OprPersonId))
        {
            writelog = true;
            return "{success:'1',msg:'',data:{model:" + JsonHelp.Convert2Json(model) + "}}";
        }
        //非本人
        else
        {
            //1.有查所有权限
            if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewCustomer_All))
            {
                //有查所有和查真电话权限 直接返回
                if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewRealTel))
                {
                    writelog = true;
                    return "{success:'1',msg:'',data:{model:" + JsonHelp.Convert2Json(model) + "}}";
                }
                //有查所有和查假电话权限 
                else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewNotRealTel))
                {
                    //如果是保护房源
                    if (house_BaseInfo.IsProtectPhone)
                    {
                        //返回假电话
                        if (Regex.IsMatch(house_BaseInfo.DisplayPhone, @"^[13]\d{1,10}$"))
                        {
                            model.Mobile = house_BaseInfo.DisplayPhone;
                            model.Phone = "";
                        }
                        else
                        {
                            model.Mobile = "";
                            model.Phone = house_BaseInfo.DisplayPhone;
                        }
                        writelog = true;
                        return "{success:'1',msg:'',data:{model:" + JsonHelp.Convert2Json(model) + "}}";
                    }
                    //如果是非保护房源
                    else {
                        writelog = true;
                        return "{success:'1',msg:'',data:{model:" + JsonHelp.Convert2Json(model) + "}}";
                    }
                } else {
                    writelog = false;
                    return "{success:0,msg:'权限设置错误，请联系管理员！',data:{permit:false}}";
                }

            }
            //2.有查城市权限
            else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewCustomer_City))
            {
                //不在同一城市
                if (!CurrentUserInfo.CityId.Equals(house_BaseInfo.CityId))
                {
                    writelog = false;
                    return "{success:0,msg:'操作失败，您无此权限，请联系管理员！',data:{permit:false}}";
                }
                //在同一城市
                else
                {
                    //有查城市和查真电话权限 直接返回
                    if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewRealTel))
                    {
                        writelog = true;
                        return "{success:'1',msg:'',data:{model:" + JsonHelp.Convert2Json(model) + "}}";
                    }
                    //有查城市和查假电话权限 
                    else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewNotRealTel))
                    {
                        //如果是保护房源
                        if (house_BaseInfo.IsProtectPhone)
                        {
                            //返回假电话
                            if (Regex.IsMatch(house_BaseInfo.DisplayPhone, @"^[13]\d{1,10}$"))
                            {
                                model.Mobile = house_BaseInfo.DisplayPhone;
                                model.Phone = "";
                            }
                            else
                            {
                                model.Mobile = "";
                                model.Phone = house_BaseInfo.DisplayPhone;
                            }
                            writelog = true;
                            return "{success:'1',msg:'',data:{model:" + JsonHelp.Convert2Json(model) + "}}";
                        }
                        //如果是非保护房源
                        else
                        {
                            writelog = true;
                            return "{success:'1',msg:'',data:{model:" + JsonHelp.Convert2Json(model) + "}}";
                        }
                    }
                    else
                    {
                        writelog = false;
                        return "{success:0,msg:'权限设置错误，请联系管理员！',data:{permit:false}}";
                    }
                }
            }
            //3.有查公司权限
            else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewCustomer_Company))
            {
                //不在同一公司
                if (!CurrentUserInfo.CompanyId.Equals(house_BaseInfo.CompanyId))
                {
                    writelog = false;
                    return "{success:0,msg:'操作失败，您无此权限，请联系管理员！',data:{permit:false}}";
                }
                //在同一公司
                else
                {
                    //有查城市和查真电话权限 直接返回
                    if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewRealTel))
                    {
                        writelog = true;
                        return "{success:'1',msg:'',data:{model:" + JsonHelp.Convert2Json(model) + "}}";
                    }
                    //有查城市和查假电话权限 
                    else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewNotRealTel))
                    {
                        //如果是保护房源
                        if (house_BaseInfo.IsProtectPhone)
                        {
                            //返回假电话
                            if (Regex.IsMatch(house_BaseInfo.DisplayPhone, @"^[13]\d{1,10}$"))
                            {
                                model.Mobile = house_BaseInfo.DisplayPhone;
                                model.Phone = "";
                            }
                            else
                            {
                                model.Mobile = "";
                                model.Phone = house_BaseInfo.DisplayPhone;
                            }
                            writelog = true;
                            return "{success:'1',msg:'',data:{model:" + JsonHelp.Convert2Json(model) + "}}";
                        }
                        //如果是非保护房源
                        else
                        {
                            writelog = true;
                            return "{success:'1',msg:'',data:{model:" + JsonHelp.Convert2Json(model) + "}}";
                        }
                    }
                    else
                    {
                        writelog = false;
                        return "{success:0,msg:'权限设置错误，请联系管理员！',data:{permit:false}}";
                    }
                }
            }
            else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewCustomer_Department))
            {
                //查部门设定
               // Chenxin.Lib.Hr.Entity.Struct_UserDepartRelation currentDep = null;
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                sb.AppendFormat("Userid = '{0}' and DepartId = '{1}' ", CurrentUserInfo.UserId, house_BaseInfo.OprDepartId);
                System.Collections.Generic.List<Chenxin.Lib.Hr.Entity.Struct_UserDepartRelation> list = Chenxin.Lib.Hr.DAL.Struct_UserDepartRelation.GetListArray(sb.ToString());
                if (list.Count > 0)
                {
                    Chenxin.Lib.Hr.Entity.Struct_UserDepartRelation currentDep = list[0];
                    //查真电话权限 直接返回
                    if (currentDep.ViewRealCustomer)
                    {
                        writelog = true;
                        return "{success:'1',msg:'',data:{model:" + JsonHelp.Convert2Json(model) + "}}";
                    }
                    //查假电话权限 
                    else if (currentDep.ViewFakeCustomer)
                    {
                        //如果是保护房源
                        if (house_BaseInfo.IsProtectPhone)
                        {
                            //返回假电话
                            if (Regex.IsMatch(house_BaseInfo.DisplayPhone, @"^[13]\d{1,10}$"))
                            {
                                model.Mobile = house_BaseInfo.DisplayPhone;
                                model.Phone = "";
                            }
                            else
                            {
                                model.Mobile = "";
                                model.Phone = house_BaseInfo.DisplayPhone;
                            }
                            writelog = true;
                            return "{success:'1',msg:'',data:{model:" + JsonHelp.Convert2Json(model) + "}}";
                        }
                        //如果是非保护房源
                        else
                        {
                            writelog = true;
                            return "{success:'1',msg:'',data:{model:" + JsonHelp.Convert2Json(model) + "}}";
                        }
                    }
                    else
                    {
                        writelog = false;
                        return "{success:0,msg:'权限设置错误，请联系管理员！',data:{permit:false}}";
                    }

                }
                else
                {
                    writelog = false;
                    return "{success:0,msg:'操作失败，您无此权限，请联系管理员！',data:{permit:false}}";
                }

            }
            else {
                writelog = false;
                return "{success:0,msg:'操作失败，您无此权限，请联系管理员！',data:{permit:false}}";
            }
        }
     }

    public string checkRegion(Chenxin.Lib.House.Entity.House_BaseInfo house_BaseInfo, Chenxin.Lib.House.Entity.Customer_BaseInfo model, out bool writelog)
    {
        bool isRegion = false;
        //是否在所管辖的区域
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        //搜出所管辖的所有区域
        sb.AppendFormat("select distinct B.RegionCtr from Struct_UserDepartRelation A left join Hr_DepartmentInfo B on A.DepartId=B.DepartId where A.IsControl='true' and A.Userid = '{0}'", CurrentUserInfo.UserId);
        DataSet ds = Chenxin.Lib.Hr.DAL.Struct_UserDepartRelation.GetTable(sb.ToString());
        System.Collections.Generic.List<string> strlist = new System.Collections.Generic.List<string>();
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            string[] st = ds.Tables[0].Rows[i]["RegionCtr"].ToString().Split(',');
            for (int j = 0; j < st.Length; j++)
            {
                if ((house_BaseInfo.Region.Trim()).Equals(st[j].Trim()))
                {
                    isRegion = true;
                    break;
                }
            }
            if (isRegion)
                break;
        }
      
        if (isRegion)
        {
            //查真电话权限 直接返回
            if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewRealTel))
            {
                writelog = true;
                return "{success:'1',msg:'',data:{model:" + JsonHelp.Convert2Json(model) + "}}";
            }
            //查假电话权限 
            else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewNotRealTel))
            {
                //如果是保护房源
                if (house_BaseInfo.IsProtectPhone)
                {
                    //返回假电话
                    if (Regex.IsMatch(house_BaseInfo.DisplayPhone, @"^[13]\d{1,10}$"))
                    {
                        model.Mobile = house_BaseInfo.DisplayPhone;
                        model.Phone = "";
                    }
                    else
                    {
                        model.Mobile = "";
                        model.Phone = house_BaseInfo.DisplayPhone;
                    }
                    writelog = true;
                    return "{success:'1',msg:'',data:{model:" + JsonHelp.Convert2Json(model) + "}}";
                }
                //如果是非保护房源
                else
                {
                    writelog = true;
                    return "{success:'1',msg:'',data:{model:" + JsonHelp.Convert2Json(model) + "}}";
                }
            }
            else
            {
                writelog = false;
                return "{success:0,msg:'权限设置错误，请联系管理员！',data:{permit:false}}";
            }
        } else 
        {
            writelog = false;
            return "{success:0,msg:'操作失败，您无此权限，请联系管理员！',data:{permit:false}}";
        }
    }
    //*******************************************
    //功能概要       ：响应字符串
    //参数説明       ：strMsg 消息 
    //返回值説明     ：void
    //功能詳細       ：权限检查
    //作成者         ：chen.yugong 
    //作成日         ：2010-05-20
    //修改者         ：
    //修改日         ：
    //*******************************************
    public override bool CustomerCheckPermint(HttpContext context, out string strMsg)
    {
        context.Response.ContentType = "text/plain";
        context.Response.AddHeader("pragma", "no-cache");
        context.Response.AddHeader("cache-control", "");
        context.Response.CacheControl = "no-cache";
        
        string op = context.Request.Params["op"];

        if (String.IsNullOrEmpty(op))
        {
            strMsg = "{success:0,msg:'操作参数有误！',data:''}";
            return false;
        }
        op = op.ToLower();

        string code = context.Request.Params["code"];
        if (op.Equals("checkpermit"))
        {
            if (String.IsNullOrEmpty(code))
            {
                strMsg = "{success:0,msg:'权限检查参数有误！',data:''}";
                return false;
            }
            code = code.ToLower();
            //权限判断
            //传递参数用
            System.Collections.Hashtable per = new System.Collections.Hashtable();
            System.Collections.Hashtable confine = new System.Collections.Hashtable();
            Chenxin.Lib.House.Entity.Customer_BaseInfo entBaseInfo = null;
            //编辑客户
            if (code.Equals("edit"))
            {
                string modelId = context.Request.Params["currentId"];
                if (String.IsNullOrEmpty(modelId))
                {
                    strMsg = "{success:0,msg:'数据已被变更，请刷新后重新操作！',data:''}";
                    return false;
                }
                entBaseInfo = Chenxin.Lib.House.DAL.Customer_BaseInfo.GetModel(new Guid(modelId));
                if (entBaseInfo == null)
                {
                    strMsg = "{success:0,msg:'数据已被变更，请刷新后重新操作！',data:''}";
                    return false;
                }

                //权限项
                per.Add("ALL", Constants.Permit_EditCustomerInfo_All);
                per.Add("CITY", Constants.Permit_EditCustomerInfo_City);
                per.Add("COMPANY", Constants.Permit_EditCustomerInfo_Company);
                per.Add("DEPART", Constants.Permit_EditCustomerInfo_Department);

                //加入范围
                confine.Add("CityId", entBaseInfo.CityId);
                confine.Add("CompanyId", entBaseInfo.CompanyId);
                confine.Add("OperatorId", entBaseInfo.OprPersonId);

                //检查权限
                if (CheckPermint(per, confine, chkDepartPermit(entBaseInfo, code), out strMsg))
                {
                    //公私客修改权限
                    if (chkPubPriv(entBaseInfo))
                    {
                        strMsg = "{success:1,msg:'',data:{permit:true}}";
                        return true;
                    }
                    else
                    {
                        strMsg = "{success:1,msg:'操作失败，您无此权限，请联系管理员！',data:{permit:false}}";
                        return false;
                    }
                }
                else
                {
                    strMsg = "{success:1,msg:'操作失败，您无此权限，请联系管理员！',data:{permit:false}}";
                    return false;
                }
            }
            //变更客户公开级别
            if (code.Equals("setpublevel"))
            {
                string modelId = context.Request.Params["currentId"];
                if (String.IsNullOrEmpty(modelId))
                {
                    strMsg = "{success:0,msg:'数据已被变更，请刷新后重新操作！',data:''}";
                    return false;
                }
                entBaseInfo = Chenxin.Lib.House.DAL.Customer_BaseInfo.GetModel(new Guid(modelId));
                if (entBaseInfo == null)
                {
                    strMsg = "{success:0,msg:'数据已被变更，请刷新后重新操作！',data:''}";
                    return false;
                }

                //权限项
                per.Add("ALL", Constants.Permit_SetPubLevel_All);
                per.Add("CITY", Constants.Permit_SetPubLevel_City);
                per.Add("COMPANY", Constants.Permit_SetPubLevel_Company);
                per.Add("DEPART", Constants.Permit_SetPubLevel_Department);

                //加入范围
                confine.Add("CityId", entBaseInfo.CityId);
                confine.Add("CompanyId", entBaseInfo.CompanyId);
                confine.Add("OperatorId", entBaseInfo.OprPersonId);

                //检查权限
                return CheckPermint(per, confine, chkDepartPermit(entBaseInfo, code), out strMsg);
            }
            //转移公开客需
            if (code.Equals("shiftpubcustomer"))
            {
                string modelId = context.Request.Params["currentId"];
                if (String.IsNullOrEmpty(modelId))
                {
                    strMsg = "{success:0,msg:'数据已被变更，请刷新后重新操作！',data:''}";
                    return false;
                }
                entBaseInfo = Chenxin.Lib.House.DAL.Customer_BaseInfo.GetModel(new Guid(modelId));
                if (entBaseInfo == null)
                {
                    strMsg = "{success:0,msg:'数据已被变更，请刷新后重新操作！',data:''}";
                    return false;
                }

                //权限项
                per.Add("ALL", Constants.Permit_ShiftPubCustomer_All);
                per.Add("CITY", Constants.Permit_ShiftPubCustomer_City);
                per.Add("COMPANY", Constants.Permit_ShiftPubCustomer_Company);
                per.Add("DEPART", Constants.Permit_ShiftPubCustomer_Department);

                //加入范围
                confine.Add("CityId", entBaseInfo.CityId);
                confine.Add("CompanyId", entBaseInfo.CompanyId);
                confine.Add("OperatorId", entBaseInfo.OprPersonId);

                //检查权限
                return CheckPermint(per, confine, chkDepartPermit(entBaseInfo, code), out strMsg);
            }
        }
        
        if("edit".Equals(op) && string.IsNullOrEmpty(code)) {

            Chenxin.Lib.House.Entity.Customer_BaseInfo model = null;
            if (context.Request["model"] != null)
            {
                model = (Chenxin.Lib.House.Entity.Customer_BaseInfo)JsonHelp.Convert2Entity(context.Request["model"], typeof(Chenxin.Lib.House.Entity.Customer_BaseInfo));
            }
            if (model == null)
            {
                strMsg = "{success:0,msg:'数据已被变更，请刷新后重新操作！',data:''}";
                return false;
            }
            Chenxin.Lib.House.Entity.Customer_BaseInfo entBaseInfo = Chenxin.Lib.House.DAL.Customer_BaseInfo.GetModel(model.CustomerId);
            if (entBaseInfo == null)
            {
                strMsg = "{success:0,msg:'数据已被变更，请刷新后重新操作！',data:''}";
                return false;
            }

            //权限判断
            //传递参数用
            System.Collections.Hashtable per = new System.Collections.Hashtable();
            System.Collections.Hashtable confine = new System.Collections.Hashtable();

            //权限项
            per.Add("ALL", Constants.Permit_EditCustomerInfo_All);
            per.Add("CITY", Constants.Permit_EditCustomerInfo_City);
            per.Add("COMPANY", Constants.Permit_EditCustomerInfo_Company);
            per.Add("DEPART", Constants.Permit_EditCustomerInfo_Department);

            //加入范围
            confine.Add("CityId", entBaseInfo.CityId);
            confine.Add("CompanyId", entBaseInfo.CompanyId);
            confine.Add("OperatorId", entBaseInfo.OprPersonId);

            //检查权限
            if (CheckPermint(per, confine, chkDepartPermit(entBaseInfo, "edit"), out strMsg))
            {
                //公私客修改权限
                if (chkPubPriv(entBaseInfo))
                {
                    strMsg = "{success:1,msg:'',data:{permit:true}}";
                    return true;
                }
                else
                {
                    strMsg = "{success:1,msg:'操作失败，您无此权限，请联系管理员！',data:{permit:false}}";
                    return false;
                }
            }
            else
            {
                strMsg = "{success:1,msg:'操作失败，您无此权限，请联系管理员！',data:{permit:false}}";
                return false;
            }
        }
        strMsg = "{success:1,msg:'',data:{permit:true}}";
        return true;
    }
   
    //*******************************************
    //功能概要       ：编辑业主权限
    //参数説明       ：context 上下文
    //返回值説明     ：bool
    //功能詳細       ：部门内有无封盘解盘权限
    //作成者         ：chen.yugong 
    //作成日         ：2010-05-19
    //修改者         ：
    //修改日         ：
    //*******************************************
    public bool chkDepartPermit(Chenxin.Lib.House.Entity.Customer_BaseInfo entBaseInfo, string op)
    {
        if (entBaseInfo.OprPersonId.Equals(CurrentUserInfo.UserId))
        {
            return true;
        }
        //检查有无权限
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.AppendFormat(" (Departid = '{0}' AND  Userid= '{1}') ", entBaseInfo.OprDepartId, CurrentUserInfo.UserId);
        System.Collections.Generic.List<Chenxin.Lib.Hr.Entity.Struct_UserDepartRelation> list = Chenxin.Lib.Hr.DAL.Struct_UserDepartRelation.GetListArray(sb.ToString());
        if (list.Count > 0)
        {
            Chenxin.Lib.Hr.Entity.Struct_UserDepartRelation struct_UserDepartRelation = list[0];
            //编辑客户
            if ("edit".Equals(op))
            {
                //私客
                if (entBaseInfo.PubLevel == 0)
                {
                    return struct_UserDepartRelation.EditPrivCustomer;
                }
                //公客
                else
                {
                    return struct_UserDepartRelation.EditPubCustomer;
                }
            }
            //变更客户公开级别
            if ("setpublevel".Equals(op))
            {
                return struct_UserDepartRelation.SetPubLevel;
            }
            //转移公开客需
            if ("shiftpubcustomer".Equals(op))
            {
                return struct_UserDepartRelation.ShiftPubCustomer;
            }
        }
        return false;
    }

    public bool chkPubPriv(Chenxin.Lib.House.Entity.Customer_BaseInfo entBaseInfo)
    {
        bool bRet = false;

        if (entBaseInfo.OprPersonId.Equals(CurrentUserInfo.UserId))
        {
            bRet = true;
        }
        else if (!CurrentUserInfo.ContainsPermit(Constants.Permit_EditCustomerInfo_Department))
        {
            //受保护客户(私客)
            if (entBaseInfo.PubLevel == 0)
            {
                bRet = CurrentUserInfo.ContainsPermit(Constants.Permit_EditPrivCustomer);
            }
            //公客
            else
            {
                bRet = CurrentUserInfo.ContainsPermit(Constants.Permit_EditPubCustomer);
            }
        }
        else
        {
            bRet = true;
        }
        return bRet;
    }
}